﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using SmartMD.DataModel.Entity;
using SmartMD.DataAccess.Service;
using SmartMD.DataModel;

namespace SmartMD.Manager.Utils
{
    public class SearchUtil
    {
        public static ObservableCollection<FollowupInfo> ForFollowupInfos(string condition)
        {
            ObservableCollection<FollowupInfo> fInfos = null;
            if (string.IsNullOrEmpty(condition))
            {
                fInfos = DataServiceManager.FollowupInfoService.GetAllFollowupInfos();            
            }
            else
            {
                List<KVP> kvps = new List<KVP>();
                string[] conditions = condition.Split(new[] { ',', '，' });
                for (int i = 0; i < conditions.Length; i++)
                {
                    KVP kvp = new KVP();
                    string[] tmpConditon = conditions[i].Split(new[] { ':', '：' });
                    if (tmpConditon.Length >= 2)
                    {
                        kvp.key = tmpConditon[0];
                        kvp.value = tmpConditon[1];
                        if (kvp.value.Trim() != "" && kvp.key.Trim() != "")
                        {
                            kvps.Add(kvp);
                        }
                    }
                }
                fInfos = DataServiceManager.FollowupInfoService.GetFollowupInfos(kvps);             
            }
            if (fInfos != null)
            {
                FollowupInfo followupInfo = null;
                for (int i = 0; i < fInfos.Count; i++)
                {
                    followupInfo = fInfos[i];
                    PatientInfo pInfo = DataServiceManager.PatientInfoService.loadPatientInfoById(followupInfo.PatientId);
                    followupInfo.PatientName = pInfo.Name;
                }
            }               
            return fInfos;
        }

        public static ObservableCollection<PatientInfo> ForPatientInfos(string condition)
        {
            ObservableCollection<PatientInfo> pInfos = null;
            if (string.IsNullOrEmpty(condition))
            {
                pInfos = DataServiceManager.PatientInfoService.loadAllPatientInfos();
              
            }
            else
            {
                List<KVP> kvps = new List<KVP>();
                string[] conditions = condition.Split(new[] { ',', '，' });
                for (int i = 0; i < conditions.Length; i++)
                {
                    KVP kvp = new KVP();
                    string[] tmpConditon = conditions[i].Split(new[] { ':', '：' });
                    if (tmpConditon.Length >= 2)
                    {
                        kvp.key = tmpConditon[0];
                        kvp.value = tmpConditon[1];
                        if (kvp.value.Trim() != "" && kvp.key.Trim() != "")
                        {
                            kvps.Add(kvp);
                        }
                    }
                }
                pInfos = DataServiceManager.PatientInfoService.GetPatientInfos(kvps);
            }
            return pInfos;
        }
    }
}
